#import "OmSettingVC.h"
#import "OmCommonButton0.h"
#import "OmSettingVCTableViewCellTypeNormal.h"
#import "RootWebViewController.h"
#import "OmChangePasswordWithoutMailVC.h"
#import "OmCheckMailVC.h"
#import "OmSettingSwitchCell.h"
#define CellReuseIDTypeNormal @"OmSettingVCTableViewCellTypeNormal"
@interface OmSettingVC () <UITableViewDelegate, UITableViewDataSource>
{
    BOOL _soundState;
    BOOL _shakeState;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) OmCommonButton0 *logOutButton;
@end
@implementation OmSettingVC
#pragma mark - data
- (NSArray *)getDataArray {
    NSArray *dataArray = @[@{@"title" : ASLocalizedString(@"铃声"),
                             @"selector" : @"onNoEventTap"
                             },
                           @{@"title" : ASLocalizedString(@"振动"),
                             @"selector" : @"onNoEventTap"
                             },
                           @{@"title" : ASLocalizedString(@"修改密码"),
                             @"selector" : @"onChangePasswordCellTap"
                             },
                           @{@"title" : ASLocalizedString(@"验证邮箱"),
                             @"selector" : @"onCheckMailCellTap"
                             },
                           @{@"title" : ASLocalizedString(@"关于我们"),
                             @"selector" : @"onAboutUsCellTap"
                             },
                           @{@"title" : ASLocalizedString(@"使用条款"),
                             @"selector" : @"onProtocolCellTap"
                             },
                           @{@"title" : ASLocalizedString(@"隐私政策"),
                             @"selector" : @"onPrivacyCellTap"
                             },
                           @{@"title" : ASLocalizedString(@"交友安全"),
                             @"selector" : @"onSafetyCellTap"
                             }];
    return dataArray;
}
#pragma mark - Event
- (void)onLogOutButtonTap {
    [self AlertWithTitle:nil message:ASLocalizedString(@"确定要退出吗？")andOthers:@[ASLocalizedString(@"取消"),ASLocalizedString(@"确定")] animated:YES action:^(NSInteger index) {
        if (index == 1) {
            [userManager logout:nil];
        }
    }];
}
- (void)onChangePasswordCellTap {
    NSLog(@"onChangePasswordCellTap");
    OmChangePasswordWithoutMailVC *vc = [[OmChangePasswordWithoutMailVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)onCheckMailCellTap {
    NSLog(@"onCheckMailCellTap");
    OmCheckMailVC *vc = [[OmCheckMailVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)onAboutUsCellTap {
    NSLog(@"onAboutUsCellTap");
    NSString *url = [URL_main stringByAppendingString:@"/admin/public/portal/article/index/id/6.html"];
    RootWebViewController *webView = [[RootWebViewController alloc] init];
    webView.url = url;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)onProtocolCellTap {
    NSLog(@"onProtocolCellTap");
    NSString *url = [URL_main stringByAppendingString:@"/admin/public/portal/article/index/id/7.html"];
    RootWebViewController *webView = [[RootWebViewController alloc] init];
    webView.url = url;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)onPrivacyCellTap {
    NSLog(@"onPrivacyCellTap");
    NSString *url = [URL_main stringByAppendingString:@"/admin/public/portal/article/index/id/2.html"];
    RootWebViewController *webView = [[RootWebViewController alloc] init];
    webView.url = url;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)onSafetyCellTap {
    NSLog(@"onSafetyCellTap");
    NSString *url = [URL_main stringByAppendingString:@"/admin/public/portal/article/index/id/18.html"];
    RootWebViewController *webView = [[RootWebViewController alloc] init];
    webView.url = url;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)onNoEventTap {
}
- (void)switchChange:(OmSettingSwitchCell *)editCell {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:editCell];
    NSInteger row = indexPath.item;
    BOOL result = row == 0?!_soundState:!_shakeState;
    [editCell setupSwitchState:result];
    if (row == 0) {
        _soundState = result;
    }else {
        _shakeState = result;
    }
}
#pragma mark - ConstructionScript
- (void)configSelf {
    self.title = ASLocalizedString(@"设置");
    self.view.backgroundColor = [UIColor whiteColor];
    [self setHiddenForTabBar:YES navigationBar:NO];
}
- (void)allocateComponents {
    self.tableView = [[UITableView alloc] init]
    .fp_delegate(self)
    .fp_dataSource(self)
    .fp_separatorStyle(UITableViewCellSeparatorStyleNone);
    self.tableView.scrollEnabled = NO;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([OmSettingVCTableViewCellTypeNormal class]) bundle:nil] forCellReuseIdentifier:CellReuseIDTypeNormal];
     [self.tableView registerNib:[UINib nibWithNibName:kOmSettingSwitchCell bundle:nil] forCellReuseIdentifier:kOmSettingSwitchCell];
    [self.view addSubview:self.tableView];
    self.logOutButton = (OmCommonButton0 *)[[OmCommonButton0 alloc] initWithTitle:ASLocalizedString(@"退出登录")]
    .fp_addTargetActionForControlEvents(self, @selector(onLogOutButtonTap), UIControlEventTouchUpInside);
    [self.view addSubview:self.logOutButton];
}
- (void)layoutComponents {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.logOutButton.mas_top);
    }];
    [self.logOutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(RPX(-5));
    }];
}
#pragma mark - TableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < 2) {
        OmSettingSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:kOmSettingSwitchCell];
        [cell setupTitle:[self getDataArray][indexPath.row][@"title"]];
        [cell setupSwitchState:indexPath.row == 0?_soundState:_shakeState];
        __weak typeof(self) weakSelf = self;
        cell.changeBlock = ^(id editCell) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf switchChange:editCell];
        };
        return cell;
    }else {
        OmSettingVCTableViewCellTypeNormal *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIDTypeNormal];
        cell.titleLabel.text = [self getDataArray][indexPath.row][@"title"];
        return cell;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self getDataArray].count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return RPX(55);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self performSelector:NSSelectorFromString([self getDataArray][indexPath.row][@"selector"])];
}
@end
